package com.guaju.yueshangLife.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.bean.AddComment;
import com.guaju.yueshangLife.bean.PicBean;
import com.guaju.yueshangLife.bean.UploadPicBean;
import com.guaju.yueshangLife.databinding.ActivityAddCommentBinding;
import com.guaju.yueshangLife.imageloader.FrescoImgLoader;
import com.guaju.yueshangLife.net.ApiClient;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.PreferenceUtil;
import com.guaju.yueshangLife.utils.ToastUtil;
import com.guaju.yueshangLife.utils.pic.QiniuUploadHelper;
import com.guaju.yueshangLife.widget.AddPicItem;
import com.guaju.yueshangLife.widget.PictureDialog;
import com.guaju.yueshangLife.widget.PictureSpinView;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;

import kr.co.namee.permissiongen.PermissionGen;

public class AddCommentActivity extends BaseActivity {
    private static final int REQUEST_PERMISSION = 101;
    private static final int SHOW_DIALOG = 0;
    private static final int DISMISS_DIALOG = 1;
    private ActivityAddCommentBinding binding;
    private int goodsId, orderId;
    ArrayList<String> imagePath = new ArrayList<String>();
    ArrayList<String> uploadedImage;
    private PicBean picBean;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SHOW_DIALOG:
                    showPleaseDialog();
                    break;
                case DISMISS_DIALOG:
                    dismissDialog();
                    break;
                default:
                    break;
            }
        }
    };
    private PictureDialog pictureDialog;
    private ImagePicker imagePicker;
    private String content;
    private float rating;

    @Override
    protected void loadViewLayout() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_add_comment);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void bindViews() {
        imagePicker = ImagePicker.getInstance();
        imagePicker.setCrop(false);
        imagePicker.setSelectLimit(9);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        goodsId = extras.getInt("goodsId");
        orderId = extras.getInt("orderId");
        String cover = extras.getString("cover");
        binding.goodsCover.setImageURI(cover);
        ApiEntity.picConfig();


    }

    @Override
    protected void setListener() {
        binding.topbar.getRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content = binding.etContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtil.show("请填写评价");
                    return;
                }
                rating = binding.ratingbar.getRating();
                if (rating == 0.0f) {
                    ToastUtil.show("亲，打个评分吧~");
                    return;
                }
                if (uploadedImage == null && !imagePath.isEmpty()) {
                    if (!imagePath.isEmpty()) {
                        mHandler.sendEmptyMessage(SHOW_DIALOG);
                        for (String str : imagePath) {
                            try {
                                String bucket = picBean.getBucket();
                                String name = System.currentTimeMillis() + ".jpg";
                                String token = QiniuUploadHelper.calcToken(name);

                                QiniuUploadHelper.uploadPicCanCancle(str, name, token);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } else if (!imagePath.isEmpty()) {

                } else {
                    ApiEntity.addComment(orderId + "", goodsId + "", content, rating + "");
                }

            }
        });
        setAddPicListener(binding.api11, AddPicItem.REQUEST_SELECT_IMAGES_CODE11, 1);
        setAddPicListener(binding.api12, AddPicItem.REQUEST_SELECT_IMAGES_CODE12, 2);
        setAddPicListener(binding.api13, AddPicItem.REQUEST_SELECT_IMAGES_CODE13, 3);
        setAddPicListener(binding.api21, AddPicItem.REQUEST_SELECT_IMAGES_CODE21, 4);
        setAddPicListener(binding.api22, AddPicItem.REQUEST_SELECT_IMAGES_CODE22, 5);
        setAddPicListener(binding.api23, AddPicItem.REQUEST_SELECT_IMAGES_CODE23, 6);
        setAddPicListener(binding.api31, AddPicItem.REQUEST_SELECT_IMAGES_CODE31, 7);
        setAddPicListener(binding.api32, AddPicItem.REQUEST_SELECT_IMAGES_CODE32, 8);
        setAddPicListener(binding.api33, AddPicItem.REQUEST_SELECT_IMAGES_CODE33, 9);
    }

    private void setAddPicListener(AddPicItem api11, final int requestSelectImagesCode11, final int i) {
        api11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photo(requestSelectImagesCode11, i);
            }
        });
    }

    private void photo(int requestCode, int i) {
        imagePicker.setSelectLimit(10 - i);
        PermissionGen.with(AddCommentActivity.this)
                .addRequestCode(REQUEST_PERMISSION)
                .permissions(
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request();
        Intent intent = new Intent(this, ImageGridActivity.class);
        startActivityForResult(intent, requestCode);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            switch (requestCode) {
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE11:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn1(images);
                    }
                    break;
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE12:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn2(images);

                    }
                    break;
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE13:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn3(images);
                    }
                    break;
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE21:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn4(images);
                    }
                    break;
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE22:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn5(images);
                    }
                    break;
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE23:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn6(images);
                    }
                    break;
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE31:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn7(images);
                    }
                    break;
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE32:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn8(images);
                    }
                    break;
                case AddPicItem.REQUEST_SELECT_IMAGES_CODE33:
                    if (data != null) {
                        ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        mountPicReturn9(images);
                    }
                    break;

            }
        }
    }

    private void mountPicReturn9(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add9pic(images);
                break;
            default:
                break;

        }

    }

    private void mountPicReturn8(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add8pic(images);
                break;
            case 2:
                add8pic(images);
                binding.api33.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                break;
            default:
                break;

        }

    }

    private void mountPicReturn7(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add7pic(images);
                break;
            case 2:
                add7pic(images);
                binding.api32.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api33.setVisibility(View.VISIBLE);
                break;
            case 3:
                add7pic(images);
                binding.api32.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api33.setVisibility(View.VISIBLE);


                binding.api33.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                break;

            default:
                break;

        }

    }

    private void mountPicReturn6(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add6pic(images);
                break;
            case 2:
                add6pic(images);
                binding.api31.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api32.setVisibility(View.VISIBLE);
                break;
            case 3:
                add6pic(images);
                binding.api31.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api32.setVisibility(View.VISIBLE);


                binding.api32.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api33.setVisibility(View.VISIBLE);

                break;
            case 4:
                add6pic(images);
                binding.api31.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api32.setVisibility(View.VISIBLE);


                binding.api32.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api33.setVisibility(View.VISIBLE);

                binding.api33.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);

                break;

            default:
                break;

        }

    }

    private void mountPicReturn5(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add5pic(images);
                break;
            case 2:
                add5pic(images);
                binding.api23.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine3.setVisibility(View.VISIBLE);
                break;
            case 3:
                add5pic(images);
                binding.api23.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine3.setVisibility(View.VISIBLE);


                binding.api31.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api32.setVisibility(View.VISIBLE);

                break;
            case 4:
                add5pic(images);
                binding.api23.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine3.setVisibility(View.VISIBLE);


                binding.api31.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api32.setVisibility(View.VISIBLE);

                binding.api32.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api33.setVisibility(View.VISIBLE);

                break;
            case 5:
                add5pic(images);
                binding.api23.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine3.setVisibility(View.VISIBLE);


                binding.api31.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api32.setVisibility(View.VISIBLE);

                binding.api32.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api33.setVisibility(View.VISIBLE);

                binding.api33.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                break;

            default:
                break;

        }

    }

    private void mountPicReturn4(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add4pic(images);
                break;
            case 2:
                add4pic(images);
                binding.api22.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api23.setVisibility(View.VISIBLE);
                break;
            case 3:
                add4pic(images);
                binding.api22.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                break;
            case 4:
                add4pic(images);
                binding.api22.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api32.setVisibility(View.VISIBLE);

                break;
            case 5:
                add4pic(images);
                binding.api22.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api32.setVisibility(View.VISIBLE);

                binding.api32.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api33.setVisibility(View.VISIBLE);
                break;
            case 6:
                add4pic(images);
                binding.api22.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api32.setVisibility(View.VISIBLE);

                binding.api32.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api33.setVisibility(View.VISIBLE);

                binding.api33.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                break;

            default:
                break;

        }

    }

    private void mountPicReturn3(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add3pic(images);
                break;
            case 2:
                add3pic(images);
                binding.api21.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api22.setVisibility(View.VISIBLE);
                break;
            case 3:
                add3pic(images);
                binding.api21.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api22.setVisibility(View.VISIBLE);
                binding.api22.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api23.setVisibility(View.VISIBLE);

                break;
            case 4:
                add3pic(images);
                binding.api21.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api22.setVisibility(View.VISIBLE);
                binding.api22.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                break;
            case 5:
                add3pic(images);
                binding.api21.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api22.setVisibility(View.VISIBLE);
                binding.api22.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api32.setVisibility(View.VISIBLE);
                break;
            case 6:

                add3pic(images);
                binding.api21.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api22.setVisibility(View.VISIBLE);
                binding.api22.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api32.setVisibility(View.VISIBLE);

                binding.api32.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.api33.setVisibility(View.VISIBLE);
                break;
            case 7:
                add3pic(images);
                binding.api21.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api22.setVisibility(View.VISIBLE);
                binding.api22.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api32.setVisibility(View.VISIBLE);

                binding.api32.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.api33.setVisibility(View.VISIBLE);
                binding.api33.addImage(images.get(6).path);
                imagePath.add(images.get(6).path);

                break;

            default:
                break;

        }

    }

    private void mountPicReturn2(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add2pic(images);
                break;
            case 2:
                add2pic(images);
                binding.api13.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine2.setVisibility(View.VISIBLE);
                break;
            case 3:
                add2pic(images);
                binding.api13.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api22.setVisibility(View.VISIBLE);

                break;
            case 4:
                add2pic(images);
                binding.api13.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api22.setVisibility(View.VISIBLE);

                binding.api22.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api23.setVisibility(View.VISIBLE);

                break;
            case 5:
                add2pic(images);
                binding.api13.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api22.setVisibility(View.VISIBLE);

                binding.api22.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api23.setVisibility(View.VISIBLE);


                binding.api23.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.llLine3.setVisibility(View.VISIBLE);
                break;
            case 6:

                add2pic(images);
                binding.api13.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api22.setVisibility(View.VISIBLE);

                binding.api22.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api23.setVisibility(View.VISIBLE);


                binding.api23.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.api32.setVisibility(View.VISIBLE);
                break;
            case 7:


                add2pic(images);
                binding.api13.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api22.setVisibility(View.VISIBLE);

                binding.api22.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api23.setVisibility(View.VISIBLE);


                binding.api23.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.api32.setVisibility(View.VISIBLE);

                binding.api32.addImage(images.get(6).path);
                imagePath.add(images.get(6).path);
                binding.api33.setVisibility(View.VISIBLE);

                break;
            case 8:
                add2pic(images);
                binding.api13.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.api22.setVisibility(View.VISIBLE);

                binding.api22.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api23.setVisibility(View.VISIBLE);


                binding.api23.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.llLine3.setVisibility(View.VISIBLE);

                binding.api31.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.api32.setVisibility(View.VISIBLE);

                binding.api32.addImage(images.get(6).path);
                imagePath.add(images.get(6).path);
                binding.api33.setVisibility(View.VISIBLE);

                binding.api33.addImage(images.get(7).path);
                imagePath.add(images.get(7).path);
                break;

            default:
                break;

        }

    }

    private void mountPicReturn1(ArrayList<ImageItem> images) {
        switch (images.size()) {
            case 0:
                break;
            case 1:
                add1Pic(images);
                break;
            case 2:
                add1Pic(images);
                binding.api12.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api13.setVisibility(View.VISIBLE);
                break;
            case 3:
                add1Pic(images);
                binding.api12.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api13.setVisibility(View.VISIBLE);

                binding.api13.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                break;
            case 4:
                add1Pic(images);
                binding.api12.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api13.setVisibility(View.VISIBLE);

                binding.api12.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api22.setVisibility(View.VISIBLE);

                break;
            case 5:

                add1Pic(images);
                binding.api12.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api13.setVisibility(View.VISIBLE);

                binding.api12.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api22.setVisibility(View.VISIBLE);


                binding.api22.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api23.setVisibility(View.VISIBLE);
                break;
            case 6:

                add1Pic(images);
                binding.api12.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api13.setVisibility(View.VISIBLE);

                binding.api12.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api22.setVisibility(View.VISIBLE);


                binding.api22.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.llLine3.setVisibility(View.VISIBLE);
                break;
            case 7:

                add1Pic(images);
                binding.api12.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api13.setVisibility(View.VISIBLE);

                binding.api12.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api22.setVisibility(View.VISIBLE);


                binding.api22.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.llLine3.setVisibility(View.VISIBLE);


                binding.api31.addImage(images.get(6).path);
                imagePath.add(images.get(6).path);
                binding.api32.setVisibility(View.VISIBLE);

                break;
            case 8:
                add1Pic(images);
                binding.api12.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api13.setVisibility(View.VISIBLE);

                binding.api12.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api22.setVisibility(View.VISIBLE);


                binding.api22.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.llLine3.setVisibility(View.VISIBLE);


                binding.api31.addImage(images.get(6).path);
                imagePath.add(images.get(6).path);
                binding.api32.setVisibility(View.VISIBLE);


                binding.api32.addImage(images.get(7).path);
                imagePath.add(images.get(7).path);
                binding.api33.setVisibility(View.VISIBLE);

                break;

            case 9:
                add1Pic(images);
                binding.api12.addImage(images.get(1).path);
                imagePath.add(images.get(1).path);
                binding.api13.setVisibility(View.VISIBLE);

                binding.api12.addImage(images.get(2).path);
                imagePath.add(images.get(2).path);
                binding.llLine2.setVisibility(View.VISIBLE);

                binding.api21.addImage(images.get(3).path);
                imagePath.add(images.get(3).path);
                binding.api22.setVisibility(View.VISIBLE);


                binding.api22.addImage(images.get(4).path);
                imagePath.add(images.get(4).path);
                binding.api23.setVisibility(View.VISIBLE);

                binding.api23.addImage(images.get(5).path);
                imagePath.add(images.get(5).path);
                binding.llLine3.setVisibility(View.VISIBLE);


                binding.api31.addImage(images.get(6).path);
                imagePath.add(images.get(6).path);
                binding.api32.setVisibility(View.VISIBLE);


                binding.api32.addImage(images.get(7).path);
                imagePath.add(images.get(7).path);
                binding.api33.setVisibility(View.VISIBLE);

                binding.api33.addImage(images.get(8).path);
                imagePath.add(images.get(8).path);
                break;
            default:
                break;

        }
    }

    private void add9pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api33.addImage(imageItem.path);
        imagePath.add(imageItem.path);
    }

    private void add8pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api32.addImage(imageItem.path);
        imagePath.add(imageItem.path);
        binding.api33.setVisibility(View.VISIBLE);
    }

    private void add7pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api31.addImage(imageItem.path);
        imagePath.add(imageItem.path);
        binding.api32.setVisibility(View.VISIBLE);
    }

    private void add6pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api23.addImage(imageItem.path);
        imagePath.add(imageItem.path);
        binding.llLine3.setVisibility(View.VISIBLE);
    }

    private void add5pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api22.addImage(imageItem.path);
        imagePath.add(imageItem.path);
        binding.api23.setVisibility(View.VISIBLE);
    }

    private void add4pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api21.addImage(imageItem.path);
        imagePath.add(imageItem.path);
        binding.api22.setVisibility(View.VISIBLE);
    }

    private void add3pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api13.addImage(imageItem.path);
        imagePath.add(imageItem.path);
        binding.llLine2.setVisibility(View.VISIBLE);
    }

    private void add2pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api12.addImage(imageItem.path);
        imagePath.add(imageItem.path);
        binding.api13.setVisibility(View.VISIBLE);
    }

    private void add1Pic(ArrayList<ImageItem> images) {
        ImageItem imageItem = images.get(0);
        binding.api11.addImage(imageItem.path);
        imagePath.add(imageItem.path);
        binding.api12.setVisibility(View.VISIBLE);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPicConfig(PicBean picBean) {
        if (picBean == null) {
            ToastUtil.show("数据异常");
        } else {
            PicBean.DomainsBean domains = picBean.getDomains();
            String https = domains.getHttps();
            String secret_key = picBean.getSecret_key();
            String access_key = picBean.getAccess_key();
            String bucket = picBean.getBucket();
            QiniuUploadHelper.init(bucket, secret_key, access_key);
            this.picBean = picBean;
            Log.e("guajujerry", "onPicConfig: " + picBean.getAccess_key() + "--" + picBean.getHotlink_prevention_key());
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPicUploaded(UploadPicBean uploadPicBean) {
        if (uploadPicBean == null) {
            ToastUtil.show("数据异常");
        } else {
            if (uploadedImage == null) {
                uploadedImage = new ArrayList<>();
            }
            String picName = uploadPicBean.getPicName();
            String avatarStr = "https://" + picBean.getDomains().getHttps() + "/" + picName;
            Log.e("guajujerry", "avatarStr: " + avatarStr);
            uploadedImage.add(avatarStr);


            if (uploadedImage.size() == imagePath.size()) {
                StringBuilder uploadStr = new StringBuilder();
                uploadStr.append("[");
                for (String str : uploadedImage) {
                    uploadStr.append(str + ",");
                }
                String s = uploadStr.toString();
                String substring = s.substring(0, s.length() - 1);
                String realPath = substring + "]";

                ApiEntity.addComment(orderId + "", goodsId + "", content, rating + "", realPath);
            }

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommented(AddComment bean) {
        int code = bean.getCode();
        String msg = bean.getMsg();
        if (200 == code) {
            mHandler.sendEmptyMessage(DISMISS_DIALOG);
            ToastUtil.show("评价成功");
            finish();
        } else {
            ToastUtil.show("商品未找到");
        }
    }

    /**
     * loading dialog
     */
    protected void showPleaseDialog() {
        if (!isFinishing()) {
            dismissDialog();
            pictureDialog = new PictureDialog(this);
            pictureDialog.show();
        }
    }

    /**
     * dismiss dialog
     */
    protected void dismissDialog() {
        try {
            if (pictureDialog != null && pictureDialog.isShowing()) {
                pictureDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
